import React, { PureComponent } from 'react';
import { connect } from 'dva';
import 'moment/locale/zh-cn';
import { Form } from 'antd';
import { ContentPackage, TabC } from '../../../components';
import style from './index.less';
import SecSearchC from './secSearch';

@connect(({ monitorpublicopinionM, loading }) => ({
  monitorpublicopinionM,
  loading,
}))
export default class Monitorpublicopinion extends PureComponent {
  state = {
    currentType: '0', // 当前类型 默认 0新闻 1公告
    analysisType: '0', // 分析类型默认值，0--全市场，1--我的关注，2--相关持仓
  };

  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
      // 查询事件标签下拉列表
      type: 'monitorpublicopinionM/comNewsEventTag',
      payload: {},
    });
    dispatch({
      /// /查询公司公告下拉列表
      type: 'monitorpublicopinionM/comAnnouncementType',
      payload: {},
    });
  }

  componentDidUpdate() {
    // 适配工作台跳转页面的情况
    const riskNews = sessionStorage.getItem('riskNews') ? JSON.parse(sessionStorage.getItem('riskNews')) : '';
    if (riskNews) {
      sessionStorage.setItem('resetFlag', '1'); // 子组件重置标志位
      this.setState(
        {
          currentType: riskNews.type == 'notices' ? '1' : '0',
        },
        () => {
          if (riskNews.type == 'notices') {
            this.myChild1 && this.myChild1.resetForm(); // 子组件重置二次查询的条件
            this.myChild1 && this.myChild1.queryTableList({ analysisType: riskNews.analysisType, affection: '2' });
          } else {
            this.myChild && this.myChild.resetForm(); // 子组件重置二次查询的条件
            this.myChild && this.myChild.queryTableList({ analysisType: riskNews.analysisType, affection: '2' });
          }
          // 清除session数据
          sessionStorage.setItem('riskNews', '');
        },
      );
    }
  }

  componentWillUnmount() {
    // 离开前清除models数据
    this.props.dispatch({
      type: 'monitorpublicopinionM/setState',
      payload: {
        comcodeList: [],
      },
    });
  }

  // 公告与新闻切换
  handleClick = (key) => {
    this.setState({
      currentType: key,
    });
  };

  render() {
    const { currentType, analysisType } = this.state;
    const tabSource = [
      {
        title: '公司新闻',
        key: '0',
        content: (
          <SecSearchC
            key={0}
            type={'0'}
            onRef={(ref) => {
              this.myChild = ref;
            }}
          />
        ),
      },
      {
        title: '公司公告',
        key: '1',
        content: (
          <SecSearchC
            type={'1'}
            key={1}
            onRef={(ref) => {
              this.myChild1 = ref;
            }}
          />
        ),
      },
    ];
    return (
      <ContentPackage
        title='主体舆情监控'
        contentHtml={
          <div className={style.mainNews}>
            <div className={style.newsList}>
              <TabC onChange={this.handleClick} activeKey={currentType} type='card' tabSource={tabSource} />
            </div>
          </div>
        }
      />
    );
  }
}
